//  发请求
var _utils = {
  middleware: function (res) {
    res = JSON.parse(res)
    if (res && res.code == 403 ) {
      window.location.href = "login.html"
    }
  },
  post: function (url, data, cb) {
    var xhr = new XMLHttpRequest()
    console.log(xhr)
    var t = this
    xhr.onload = function() {
      var r = this.responseText ? this.responseText: null;
      t.middleware(r);
      cb(JSON.parse(r));
    }
    xhr.open('POST', _config.base + url)
    xhr.setRequestHeader("Content-Type", "application/json")
    xhr.send(JSON.stringify(data))
  },
  put: function (url, data, cb) {
    var xhr = new XMLHttpRequest()
    console.log(xhr)
    var t = this
    xhr.onload = function() {
      var r = this.responseText ? this.responseText: null;
      t.middleware(r);
      cb(JSON.parse(r));
    }
    xhr.open('PUT', _config.base + url)
    xhr.setRequestHeader("Content-Type", "application/json")
    xhr.send(JSON.stringify(data))
  },
  del: function (url, data, cb) {
    var xhr = new XMLHttpRequest()
    console.log(xhr)
    var t = this
    xhr.onload = function() {
      var r = this.responseText ? this.responseText: null;
      t.middleware(r);
      cb(JSON.parse(r));
    }
    xhr.open('DELETE', _config.base + url)
    xhr.setRequestHeader("Content-Type", "application/json")
    xhr.send(JSON.stringify(data))
  },
  get: function(url, data, cb) {
    var xhr = new XMLHttpRequest()
    console.log(xhr)
    var t = this
    xhr.onload = function() {
      var r = this.responseText ? this.responseText: null;
      t.middleware(r);
      cb(JSON.parse(r));
    }
    var _params = [];
    for (var i in data ) {
      _params.push(i + '=' + data[i])
    }
    if (_params.length > 0) { url += '?' + _params.join('&') }
    xhr.open('GET', _config.base + url)
    xhr.setRequestHeader("Content-Type", "application/json")
    xhr.send()
  },
  //  转换成驼峰
  toCamelCase: function(str) {
    return str.replace(/_(\w)/g, function() {
        return arguments[1].toUpperCase()
    })
  },
  //  向上找对应元素
  closest: function (obj, attr) {
    var _judge = (obj) => {
      var _r = true
      for (var key in attr) {
        _r &= obj[key] == attr[key]
      }
      return _r
    }
    while (!_judge(obj)) {
      obj = obj.parentNode
    }
    return obj
  },
  openMask: function () {
    document.getElementById('mask').style.display = "block";
  },
  closeMask: function () {
    document.getElementById('mask').style.display = "none";
  },
  sortSelect: function (_arr) {
    return _arr && _arr.sort(function(a, b) {
      return b.id - a.id
    })
  },
  formatTNum: function (num) {
    var parts = num.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
  },
  logout: function (num) {
    _utils.get('/api/logout', {} , (res) => {
      _utils.delCookie('usr');
      _utils.delCookie('Cookie');
      window.location.href = "login.html";
    })
  },
  setCookie: function (name,value) { 
    var h = 8; 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + h*60*60*1000); 
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
  },
  getCookie: function(name) { 
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)){
      return unescape(arr[2]); 
    } else {
      return null; 
    }        
  },
  delCookie: function (name) { 
    var exp = new Date(); 
    exp.setTime(exp.getTime() - 1); 
    var cval= _utils.getCookie(name); 
    console.log(exp)
    if(cval!=null) 
        document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
  },
  commonInit: function () {
    document.getElementById('usr') && ( document.getElementById('usr').innerHTML = _utils.getCookie('usr') );
  }
}
window.comm = {};
_utils.commonInit();
window.comm.logout = _utils.logout;